package com.woniu.learnredirect.controller;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * created by woniu201
 * created in 2018/4/12
 */
//@RestController
public class RedirectController {
    /**
     * Get重定向测试
     */
    private String destServerAddr = "http://192.168.12.1";
    @RequestMapping(value = {"/*","/*/*","/*/*/*"}, method = RequestMethod.GET)
    public void test1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得请求的url
        String url = request.getRequestURI();
        System.out.println(url);
        if (url.equals("/test1")){
            response.sendRedirect("http://localhost:8080/gettest");
        }else if (url.equals("/test2")){
            response.sendRedirect("http://localhost:8080/posttest");
        }
        //response.sendRedirect("http://www.baidu.com");
    }

    @RequestMapping(value = {"/*","/*/*","/*/*/*"}, method = RequestMethod.POST)
    public void test2(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得请求的url
        String url = request.getRequestURI();

        if (url.equals("/test1")){
            response.sendRedirect("http://localhost:8080/gettest");//这种方式为不带参数的重定向
        }else if (url.equals("/test2")){
            response.sendRedirect("http://localhost:8080/posttest");
        }
        //response.sendRedirect("http://www.baidu.com");
    }

    @RequestMapping(value = {"/11"}, method = RequestMethod.GET)
    public ModelAndView test3(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获得请求的url
        String url = request.getRequestURI();
        System.out.println(url);

        return new ModelAndView("/test1");
    }
}
